<?php

/**
 * This is the model class for table "tbl_warehouse".
 *
 * The followings are the available columns in table 'tbl_warehouse':
 * @property string $warehouseid
 * @property string $warehousedesc
 * @property integer $status
 * @property string $empno
 */
class Warehouse extends CActiveRecord
{
    /**
     * Returns the static model of the specified AR class.
     * @return Warehouse the static model class
     */
    public static function model($className=__CLASS__)
    {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName()
    {
        return 'tbl_warehouse';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules()
    {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('warehouseid', 'required'),
            array('warehouseid','unique','className'=>'Warehouse','attributeName'=>'warehouseid'),
            array('status', 'numerical', 'integerOnly'=>true),
            array('warehouseid, empno', 'length', 'max'=>4),
            array('warehousedesc', 'length', 'max'=>75),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('warehouseid, warehousedesc, status, empno', 'safe', 'on'=>'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations()
    {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels()
    {
        return array(
            'warehouseid' => 'ID',
            'warehousedesc' => 'Description',
            'status' => 'Status',
            'empno' => 'Employee',
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search()
    {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria=new CDbCriteria;

        $criteria->compare('warehouseid',$this->warehouseid,true);
        $criteria->compare('warehousedesc',$this->warehousedesc,true);
        $criteria->compare('status',$this->status);
        $criteria->compare('empno',$this->empno,true);

        return new CActiveDataProvider($this, array(
            'criteria'=>$criteria,
        ));
    }

    public function getStatusList(){
        return array(
            0=>'Active',
            -1=>'Inactive'
        );
    }
    public function getStatusText(){
        return isset($this->statusList[$this->status])?$this->statusList[$this->status]:NULL;
    }
    public function getEmployeeList(){
        $data=Yii::app()->db->createCommand('select firstname,lastname,empno from tbl_employees')->queryAll();
        $rs=array();
        $rs['']="N/A";
        if(count($data)>0){
            foreach($data as $item){
                $rs[$item['empno']]=$item['firstname'].' '.$item['lastname'];
            }
        }
        return $rs;
    }
    public function getEmployeeName(){
        if($this->empno!=NULL){
            $data=Yii::app()->db->createCommand('select firstname,lastname from tbl_employees where empno=:empno')->bindValue('empno', $this->empno)->queryRow();
            return $data['firstname'].' '.$data['lastname'];
        }
        return NULL;
    }

    public function getList(){
        $data=Yii::app()->db->createCommand('select * from tbl_warehouse')->queryAll();
        $rs=array(

        );

        if(count($data)>0){
            foreach($data as $i=>$item){
                $rs[$item['warehouseid']]=$item['warehousedesc'].' '.$item['warehouseid'];
            }
        }
        return $rs;
    }

    public function getText($id){
        $data=Yii::app()->db->createCommand('select warehousedesc from tbl_warehouse where warehouseid=:id')->bindValue('id',$id)->queryScalar();
        if($data!=false){
            return $data;
        }
        return $id;
    }
    public static function getDescription($id)
    {
        $data=Yii::app()->db->createCommand('select warehousedesc from tbl_warehouse where warehouseid=:id')->bindValue('id',$id)->queryScalar();
        if($data!=false){
            return $id.' - '.$data;
        }
        return $id;
    }
}
